const Actions = require('./actions');
const DB = require('../../app/db');
import {Toast} from 'saltui';

const {fromJS} = require('immutable');

const storageUser = nattyStorage({
  type: 'localStorage',  // 缓存方式, 默认为'localStorage'
  key: 'sskj-dingtalk-user',     // !!! 唯一必选的参数, 用于内部存储 !!!
});

module.exports = Reflux.createStore({

  listenables: [Actions],
  data: {
    user: fromJS(storageUser),
  },

  //钉盘授权
  onDpAuth: function (params, cb) {
    let t = this;

    Toast.show({
      type: 'loading',
      content: '加载中...',
      autoHide: false,
      hasMask: true,
    });
    DB.dp.get().then(
      function (content) {
        Toast.hide(function () {
        });
        if (content.errcode == "0") {
          let spaceid = content.spaceid;

          DB.dp.auth({userid: t.data.user.get('userid')}).then(
            function (content) {
              if (content.errcode == "0") {
                cb(spaceid, params);
              }
            },
            function (error) {
              alert('授权钉盘失败!');
              console.log(error);
            })
        }

      }, function (error) {

        console.log(error);
        Toast.hide(function () {
          alert('获取盯盘ID失败:' + JSON.stringify(error));
        });
      });

    t.updateComponent();
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  },
})
;
